package com.tabuto.jizy.dao.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class AbstractJDBCDao {
	
	
	public Connection getConnection(){
		Connection conn = null;
		try{
			Class.forName("org.sqlite.JDBC");
			//Apro una connessione al file del database
			conn = 
				DriverManager.getConnection(
						"jdbc:sqlite:"+JDBCConstant.SQLITE_DB_FILE_PATH);
			if(conn == null){
				throw new RuntimeException("Unable to open DataBase connection");
			}
			
		} catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("An error occours: ",e);
		}
		return conn;
	}
	
	public Connection getSQLiteConnection(){
		Connection conn = null;
		try{
			Class.forName("org.sqlite.JDBC");
			//Apro una connessione al file del database
			conn = 
				DriverManager.getConnection(
						"jdbc:sqlite:"+JDBCConstant.SQLITE_DB_FILE_PATH);
			if(conn == null){
				throw new RuntimeException("Unable to open DataBase connection");
			}
			
		} catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("An error occours: ",e);
		}
		return conn;
	}
	
	public Connection getMySQLConnection(){
		Connection conn = null;
		try{
			Class.forName(JDBCConstant.MYSQL_DRIVER_NAME);
			
			conn = 
				DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/"+JDBCConstant.MYSQL_DB_NAME, 
						JDBCConstant.MYSQL_USER_NAME, JDBCConstant.MYSQL_PASSWORD );
			
			if(conn == null){
				throw new RuntimeException("Unable to open DataBase connection");
			}
			
		} catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("An error occours: ",e);
		}
		return conn;
	}

}
